package com.ruanyun.tio.net.common.utils;

import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * @Author: chail
 * @Date: 2019/3/13
 * @Description:
 */
public class BinaryHexUtil {
    private static final String BIN_SEPARATOR = " ";
    /**
     * 二进制转16进制
     * @param str
     * @return
     */
    public static String binaryToHex(byte[] str) {
        StringBuilder stringBuilder = new StringBuilder("");

        if (str == null || str.length <= 0) {
        }
        for (int i = 0; i < str.length; i++) {
            int v = str[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    /**
     * 16进制转二进制
     * @param hexString
     * @return
     */
    public static byte[] hexToBinary(String hexString) {


		if (ToolUtil.isEmpty(hexString)) {
			return null;
		}

        hexString = hexString.replaceAll(" ", "");

        int len = hexString.length();
        int index = 0;


        byte[] bytes = new byte[len / 2];

        while (index < len) {

            String sub = hexString.substring(index, index + 2);

            bytes[index / 2] = (byte) Integer.parseInt(sub, 16);

            index += 2;
        }


        return bytes;
    }
    /**
     * 16进制转换成为String类型字符串
     * @param hexStr
     * @return
     */
    public static String hexToString(String hexStr) {
        if (hexStr == null || hexStr.equals("")) {
            return null;
        }
        hexStr = hexStr.replace(" ", "");
        byte[] baKeyword = new byte[hexStr.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            hexStr = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return hexStr;
    }
    /**
     * 16进制直接转换成为字符串(无需Unicode解码)
     * @param hexStr
     * @return
     */
    public static String hexToString2(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }
    /**
     * 字符串转换成为16进制(无需Unicode编码)
     * @param str
     * @return
     */
    public static String stringToHex(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * int类型转二进制
     * @param value
     * @return
     */
    public static byte[] intToBinary(int value){
        byte[] byteArray = new byte[4];
        byteArray[0] = (byte) (value & 0xFF);
        byteArray[1] = (byte) (value >> 8  & 0xFF);
        byteArray[2] = (byte) (value >> 16 & 0xFF);
        byteArray[3] = (byte) (value >> 24 & 0xFF);
        return byteArray;
    }

    /**
     * 二进制转int
     * @param byteArray
     * @return
     */
    public static int binaryToInt(byte[] byteArray){
//        if(byteArray.length != 4){
//            return 0;
//        }
        int value = byteArray[0] & 0xFF;
        value |= byteArray[1] << 8;
        value |= byteArray[2] << 16;
        value |= byteArray[3] << 24;
        return value;
    }
    /**
     * 二进制字符串转换为普通字符串
     * @param binaryStr 二进制字符串
     * @return String 普通字符串
     * @Auth zongf
     * @Date 2019-05-07
     */
    public static String toString(String binaryStr) {

        if (binaryStr == null) return null;

        String[] binArrays = binaryStr.split(BIN_SEPARATOR);


        StringBuffer sb = new StringBuffer();
        for (String binStr : binArrays) {
            char c = binstrToChar(binStr);
            sb.append(c);
        }
        return sb.toString();
    }
    /**
     * 将二进制转换成字符
     * @param binStr 二进制字符串
     * @return char
     * @Auth zongf
     * @Date 2019-05-07
     */
    private static char binstrToChar(String binStr){
        int[] temp=binstrToIntArray(binStr);
        int sum=0;
        for(int i=0; i<temp.length;i++){
            sum +=temp[temp.length-1-i]<<i;
        }
        return (char)sum;
    }
    /**
     * 二进制字符转换为int数组
     * @param binStr 二进制字符串
     * @return int[]
     * @Auth zongf
     * @Date 2019-05-07
     */
    private static int[] binstrToIntArray(String binStr) {
        char[] temp=binStr.toCharArray();
        int[] result=new int[temp.length];
        for(int i=0;i<temp.length;i++) {
            result[i]=temp[i]-48;
        }
        return result;
    }
    public static void main(String[] args) {
        System.out.println(intToBinary(9));
        System.out.println(Integer.toBinaryString(Integer.parseInt("15",16)));
    }
}
